package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.Practitioner;
import com.banksoft.cps.bean.query.PractitionerQuery;
import com.banksoft.cps.bean.warpper.PractitionerWarpper;
import com.banksoft.cps.dao.FileInfoDao;
import com.banksoft.cps.dao.PractitionerDao;
import com.banksoft.cps.enums.DataFlag;
import com.banksoft.cps.service.PractitionerService;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2017/12/27.
 */
@Service
public class PractitionerServiceImpl implements PractitionerService {

    @Autowired
    PractitionerDao practitionerDao;
    @Autowired
    FileInfoDao fileInfoDao;

    @Override
    public PractitionerWarpper findById(String id) {
        if(StringUtils.isNotBlank(id)){
            PractitionerQuery query = new PractitionerQuery();
            query.setId(id);
            List<Practitioner> list = practitionerDao.list(query);
            Practitioner practitioner = list.get(0);
            PractitionerWarpper warpper =  new PractitionerWarpper();
            BeanUtils.copyProperties(practitioner,warpper);
            String files = practitioner.getQualificationDoc();
            if (StringUtils.isNotBlank(files)) {
                String[] fileIdsArray = files.split(",");
                List fileIds = Arrays.asList(fileIdsArray);
                List fileInfos = fileInfoDao.queryFiles(fileIds);
                warpper.setFileInfoList(fileInfos);
            }
            return warpper;
        }
        return null;
    }

    @Override
    public List<Practitioner> list(PractitionerQuery query) {
        return practitionerDao.list(query);
    }

    @Override
    public long count(PractitionerQuery query) {
        return practitionerDao.count(query);
    }

    @Override
    public void save(Practitioner practitioner) {
        if(StringUtils.isNotBlank(practitioner.getId())){
            practitionerDao.update(practitioner);
        }else {

            practitioner.setDataFlag(DataFlag.y.name());
            practitioner.setId(UUIDFactory.random());
            practitioner.setCreateTime(System.currentTimeMillis());
            practitionerDao.insert(practitioner);
        }
    }

    @Override
    public void delete(String[] id) {
        practitionerDao.delete(id);
    }

    @Override
    public void setQDoc(String id, String qualificationDoc) {
        practitionerDao.setQDoc(id,qualificationDoc);
    }

    @Override
    public void updateStatus(String[] ids, String status) {
        practitionerDao.updateStatus(ids,status);
    }
}
